from typing import List, Dict

from openai import OpenAI, AsyncOpenAI

ai_client = OpenAI(api_key="sk-98d398b3884f49b6985a5e49f01db404", base_url="https://api.deepseek.com")
as_ai_client = AsyncOpenAI(
    api_key="sk-98d398b3884f49b6985a5e49f01db404",
    base_url="https://api.deepseek.com"
)
async def AiCli(messages:List[Dict[str,str]], user:str,model:str='deepseek-chat', stream:bool=False,max_tokens:int=1024):
    """
    AI客户端，根据输入获得对应的输出，默认使用DS和非流式
    :return:
    """
    response = ai_client.chat.completions.create(
        model=model,
        messages=messages,
        stream=stream,
        max_tokens=max_tokens,
        user=user
    )
    return response.choices[0].message.content

async def AsyncAiCli(messages: List[Dict[str, str]], user: str, model: str = 'deepseek-chat',
                stream: bool = False, max_tokens: int = 1024):
    """
    真正的异步AI客户端
    """
    try:
        response = await as_ai_client.chat.completions.create(
            model=model,
            messages=messages,
            stream=stream,
            max_tokens=max_tokens,
            user=user
        )
        return response.choices[0].message.content
    except Exception as e:
        print(f"API请求失败: {e}")
        return f"Error: {e}"